package com.wangwenjun.concurrency.chapter14.happenbefore;

import java.util.concurrent.TimeUnit;

public class UseVolatile {

    private static Monitor monitor = new Monitor();

    public static void main(String[] args) {


        new Thread(()->{
            while (monitor != null){
                // 只有读对象.volatile域的时候,monitor才具有可见性,否则会死循环
                if(monitor.flag){

                }
            }
            System.out.println("end");
        }).start();


        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        monitor = null;
    }

}

class Monitor{

    public volatile boolean flag = true;

}
